package com.happy.cat.project.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happy.cat.common.utils.AesUtils;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.project.system.domain.*;
import com.happy.cat.project.system.domain.vo.UserUpdateByNumbers;
import com.happy.cat.project.system.mapper.UserAccountMapper;
import com.happy.cat.project.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 用户注册的账号Service业务层处理
 *
 * @author verne
 * @date 2020-08-04
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements IUserAccountService {

    @Autowired
    private IUserImAuthorityService imAuthorityService;

    @Autowired
    private IHappyCatUserService catUserService;

    private static final String AESKEY = "F4926CA4E64E485F";

    @Autowired
    private IUserExperienceService experienceService;

    @Autowired
    private IUserLightStackService stackService;

    @Autowired
    private IUserBalancePetValueDetailService balancePetValueDetailService;

    @Autowired
    private IUserPetValueService petValueService;

    @Override
    public boolean saveItem(UserAccount userAccount) {
        HappyCatUser user = new HappyCatUser();
        user.setCreateTime(new Date());
        user.setPhone(userAccount.getPhone());
        user.setRealName(userAccount.getNick());
        user.setStatus(2);
        catUserService.saveOrUpdate(user);

        userAccount.setAccountName(userAccount.getPhone());
        userAccount.setPassword(AesUtils.encrypt(userAccount.getPassword(), AESKEY));
        userAccount.setUserId(user.getId());
        userAccount.setCreateTime(new Date());
        userAccount.setStatus(2);

        UserImAuthority imAuthority = userAccount.getUserImAuthority();
        imAuthority.setUserId(user.getId());
        imAuthorityService.save(imAuthority);
        return this.save(userAccount);
    }

    @Override
    public boolean updateByItem(UserAccount userAccount) {
        imAuthorityService.updateById(userAccount.getUserImAuthority());
        if(StringUtils.isNotEmpty(userAccount.getNewPassword())){
            userAccount.setPassword(AesUtils.encrypt(userAccount.getNewPassword(), AESKEY));
        }
        return this.updateById(userAccount);
    }

    @Override
    public boolean updateByNumbers(UserUpdateByNumbers userUpdateByNumbers) {
        UserAccount userAccount = this.getById(userUpdateByNumbers.getUserId());

        /*修改猫币 */
        if(userUpdateByNumbers.getCatCoin()!=null){
            UserBalancePetValueDetail balancePetValueDetail = new UserBalancePetValueDetail();
            balancePetValueDetail.setAmountType(1);
            balancePetValueDetail.setSymbol(userUpdateByNumbers.getCatCoin()>=0?1:0);
            balancePetValueDetail.setAccountId(userAccount.getId());
            balancePetValueDetail.setChangeTime(new Date());
            balancePetValueDetail.setCurrentAmount(userAccount.getCatCoinBalance());
            balancePetValueDetail.setAmount(userUpdateByNumbers.getCatCoin());
            balancePetValueDetail.setText("管理员调整");
            balancePetValueDetail.setType(0L);
            balancePetValueDetailService.save(balancePetValueDetail);

            userAccount.setCatCoinBalance(userUpdateByNumbers.getCatCoin() + userAccount.getCatCoinBalance());
        }

        /*修改猫粮*/
        if(userUpdateByNumbers.getCatFood()!=null){

            UserBalancePetValueDetail balancePetValueDetail = new UserBalancePetValueDetail();
            balancePetValueDetail.setAmountType(0);
            balancePetValueDetail.setSymbol(userUpdateByNumbers.getCatFood()>=0?1:0);
            balancePetValueDetail.setAccountId(userAccount.getId());
            balancePetValueDetail.setChangeTime(new Date());
            balancePetValueDetail.setCurrentAmount(userAccount.getBalance());
            balancePetValueDetail.setAmount(userUpdateByNumbers.getCatFood());
            balancePetValueDetail.setText("管理员调整");
            balancePetValueDetail.setType(0L);
            balancePetValueDetailService.save(balancePetValueDetail);

            userAccount.setBalance(userAccount.getBalance()+userUpdateByNumbers.getCatFood());
        }
        /*修改宠爱值*/
        if(userUpdateByNumbers.getPetValue()!=null){

            UserPetValue petValue = new UserPetValue();
            petValue.setAccountId(userAccount.getId());
            petValue.setChangeTime(new Date());
            petValue.setCurrentScore(userAccount.getPetValueBalance());
            petValue.setScore(userUpdateByNumbers.getPetValue());
            petValue.setText("管理员调整");
            petValueService.save(petValue);

            userAccount.setPetValueBalance(userAccount.getPetValueBalance()+userUpdateByNumbers.getPetValue());
        }

        /*修改经验值*/
        if(userUpdateByNumbers.getExp()!=null){
            UserExperience experience = new UserExperience();
            experience.setAccountId(userAccount.getId());
            experience.setCreateTime(new Date());
            experience.setCurrentExperience(userAccount.getExp());
            experience.setExperiencePoints(userUpdateByNumbers.getExp());
            experience.setSource(0);
            experience.setRemarks("操作人员："+ SecurityUtils.getUsername()+"操作备注:"+ userUpdateByNumbers.getRemarks());
            experienceService.save(experience);

            userAccount.setExp(userAccount.getExp()+userUpdateByNumbers.getExp());
        }
        /*修改荧光棒*/
        if(userUpdateByNumbers.getStick()!=null){
            UserLightStack lightStack = new UserLightStack();
            lightStack.setAccountId(userAccount.getId());
            lightStack.setChangeTime(new Date());
            lightStack.setCurrentSroce(userAccount.getLightStick());
            lightStack.setSroce(userUpdateByNumbers.getStick());
            lightStack.setType(0);
            lightStack.setRemarks("操作人员："+ SecurityUtils.getUsername()+"操作备注:"+ userUpdateByNumbers.getRemarks());
            stackService.save(lightStack);

            userAccount.setLightStick(userAccount.getLightStick()+userUpdateByNumbers.getStick());
        }
        userAccount.setRemarks(userUpdateByNumbers.getRemarks());
        return updateById(userAccount);
    }

    @Override
    public boolean updateByStatus(Long id, int status) {
        return this.lambdaUpdate().eq(UserAccount::getId,id).set(UserAccount::getStatus,status).update();
    }
}
